package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongodto.RecommendUserDto;
import com.tanhua.model.vo.PageResult;
import io.netty.util.internal.StringUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 新增用户详情表
     *
     * @param userInfo
     */
    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 修改用户详情表
     *
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 根据id查询用户信息
     *
     * @param userID
     * @return
     */
    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    /**
     * 获取当前用户推荐朋友的详情信息
     * @return
     */
    @Override
    public Map<Long,UserInfo> findByIds(UserInfo u, List<Long> ids) {

        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.in(UserInfo::getId,ids);

        if (u != null){
            queryWrapper.eq(u.getGender() != null,UserInfo::getGender,u.getGender())
                    .lt(u.getAge() != null,UserInfo::getAge,u.getAge())
                    .like(u.getCity() != null,UserInfo::getCity,u.getCity())
                    .eq(u.getEducation() != null,UserInfo::getEducation,u.getEducation());
        }

        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);

        //也可以使用工具类将List集合转成Map集合
        //Map<Long, UserInfo> objectMap = CollUtil.fieldValueMap(userInfoList, "id");

        Map map = new HashMap();
        for (UserInfo userInfo : userInfoList) {
            map.put(userInfo.getId(),userInfo);
        }
        return map;
    }

    /**
     * 修改头像
     *
     * @param url
     * @param userId
     */
    @Override
    public void updateHeader(String url, Long userId) {
        userInfoMapper.updateHeader(url,userId);
    }

    /**
     * 分页查询当前用户的好友详情信息(模糊查询)
     *
     * @param friendIds
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    @Override
    public IPage<UserInfo> findFriendById(List<Long> friendIds, int page, int pagesize, String keyword) {
        //创建构造
        Page<UserInfo> userInfoPage = new Page<>(page,pagesize);

        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(UserInfo::getId,friendIds)
                .like(keyword != null,UserInfo::getNickname,keyword);

        userInfoMapper.selectPage(userInfoPage,queryWrapper);

        return userInfoPage;
    }
}
